﻿//using IntercamberWeb.Filters;
//using IntercamberWeb.Models;

using System.Web.Mvc;
using CML.Intercamber.Business;
using CML.Intercamber.Business.Dao;
using CML.Intercamber.Business.Helper;
using CML.Intercamber.Web.Helpers;

namespace CML.Intercamber.Web.Controllers
{
    [Authorize]
    //[InitializeSimpleMembership]
    public class HomeController : BaseController
    {

        public ActionResult Index()
        {
            ViewBag.Establishments = EstablishmentHelper.AllEstablishments;
            Users connectedUser = ConnectedUserHelper.ConnectedUser;
            ViewBag.ListNews = NewsDao.ListNewsForUser(connectedUser);

            return View();
        }

        public ActionResult Contact()
        {
            var agent = ConnectedUserHelper.ConnectedUser;
            if (agent == null)
                return RedirectToAction("Login", "Account");
            return View();
        }

        [HttpPost]
        public ActionResult Contact(string subject, string message)
        {
            var user = ConnectedUserHelper.ConnectedUser;
            if (user == null)
                return RedirectToAction("Login", "Account");
            if (string.IsNullOrEmpty(subject) || string.IsNullOrEmpty(message))
            {
                ViewBag.WarnMessage = Resources.Intercamber.Error_SubjectMessageEmpty;
            }
            else
            {
                string mailBody = "Sent by " + user.FirstName + " " + user.LastName + " (" + user.Email+")\r\n\r\n" + message;
                bool sent = MailHelper.SendMail(MailHelper.SupportMailAdress, "[CONTACT] " + subject, mailBody);
                if (sent)
                    ViewBag.OkMessage = true;
                else
                    ViewBag.WarnMessage = Resources.Intercamber.Error_CannotSendMail;
            }
            return View();
        }
    }
}
